package com.grapevine.contact;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.grapevine.contact.adapter.ContactAdapter;
import com.grapevine.contact.util.ContactPhotoLoader;

public class Contact extends ListActivity implements OnItemLongClickListener
{
	private ContactAdapter mAdapter;

	private Cursor mCursor;

	private ContactPhotoLoader mContactPhotoLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_contact);

		mContactPhotoLoader = new ContactPhotoLoader(this, R.drawable.ic_contact_list_picture);

		// mCursor =
		// getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
		// null,
		// Contacts.HAS_PHONE_NUMBER + " = '1' and " + Contacts.IN_VISIBLE_GROUP
		// + " = '1'", null,
		// ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
		mCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
				Contacts.HAS_PHONE_NUMBER + " = '1'", null,
				ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
		startManagingCursor(mCursor);
		mAdapter = new ContactAdapter(this, mCursor, mContactPhotoLoader);
		getListView().setOnScrollListener(mAdapter);
		getListView().setOnItemLongClickListener(this);
		getListView().setAdapter(mAdapter);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		mContactPhotoLoader.pause();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);

		Cursor cursor = (Cursor) mAdapter.getItem(position);

		long nId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));

		Uri scan = ContentUris.withAppendedId(Contacts.CONTENT_URI, nId);
		Intent intent = new Intent(Intent.ACTION_VIEW, scan);
		startActivity(intent);

		// Intent intent = new Intent(this, DetialActivity.class);
		// intent.putExtra(ModeManager.MODE, ModeManager.CONTACT_DETAIL);
		// intent.putExtra(DetialActivity.ID, nId);
		// startActivity(intent);

	}

	@Override
	protected void onResume()
	{
		mContactPhotoLoader.resume();
		mCursor.requery();
		mAdapter.notifyDataSetChanged();
		super.onResume();
	}

	private static final String[] mOperator = { "查看", "编辑", "删除" };

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
	{
		Cursor cursor = (Cursor) mAdapter.getItem(position);
		String title = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

		final long personId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, mOperator);

		new AlertDialog.Builder(this).setTitle(title).setAdapter(adapter, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				Intent intent;
				Uri data = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);

				switch (which)
				{
				case 0:// 查看

					intent = new Intent(Intent.ACTION_VIEW, data);
					startActivity(intent);
					break;

				case 1:// 编辑
					intent = new Intent(Intent.ACTION_EDIT, data);
					startActivity(intent);
					break;

				case 2: // 删除
					getContentResolver().delete(data, null, null);
					mCursor.requery();
					mAdapter.notifyDataSetChanged();
					break;

				default:
					break;
				}

			}
		}).create().show();
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, Menu.FIRST, 0, "添加联系人").setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, Menu.FIRST + 1, 0, "导入/导出").setIcon(R.drawable.ic_menu_import_export);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{

		boolean ret = true;
		Intent intent;

		switch (item.getItemId())
		{
		case Menu.FIRST:// 添加联系人
			intent = new Intent(Intent.ACTION_INSERT);
			intent.setType("vnd.android.cursor.dir/contact");
			startActivity(intent);
			break;
		case Menu.FIRST + 1: // 导入导出
			importOrOutputContact();
			break;

		default:
			ret = super.onOptionsItemSelected(item);
			break;
		}
		return ret;
	}

	private static final String[] mOperatorVcard = { "从SD卡导入", "导出到SD卡" };

	private void importOrOutputContact()
	{
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, mOperatorVcard);

		new AlertDialog.Builder(this).setTitle("导入/导出联系人").setAdapter(adapter, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				switch (which)
				{
				case 0:// 导入

					break;

				case 1:// 导出
					break;

				default:
					break;
				}
			}
		}).setNegativeButton("取消", null).create().show();
	}

}